package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.OrderSaleAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.OderSaleContent;
import com.isunland.managesystem.entity.OderSaleOriginal;
import com.isunland.managesystem.entity.OrderQueryParams;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

public class OrderSaleListFragment extends BaseListFragment {
  private static final int ADD_ORDER = 0;
  private static final int SEARCH_ORDER = 1;
  private static final int DETAIL_ORDER = 2;
  private OrderSaleAdapter mAdapter;
  private ArrayList<OderSaleContent> mList;
  private StringBuffer buffer = new StringBuffer();
  private OrderQueryParams mQueryParams;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.order_sale);
  }

  @Override public void initData() {
    super.initData();
    mQueryParams = new OrderQueryParams();
    mQueryParams.setBeginregDate(MyDateUtil.getFirstDay());
    mQueryParams.setEndregDate(MyDateUtil.getLastDay());
    mQueryParams.setDataStatusName(getString(R.string.all));
    buffer = buffer.append(mCurrentUser.getMemberCode()).append("-DJLB-OUT-XSDDCK");
  }

  @Override public String getUrl() {

    return ApiConst.URL_ORDER_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("beginregDate", mQueryParams.getBeginregDate());
    String newEnd = MyDateUtil.getAfterDayTotring(mQueryParams.getEndregDate(), 1);
    params.put("endregDate", newEnd);
    params.put("moutbillStatus", mQueryParams.getDataStatus());
    params.put("customerName", mQueryParams.getCustomerName());
    params.put("moutkindCode", buffer.toString());
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("ifUseRoleType", "false");
    params.put("dataRange", "myData");//只能查出自己的销售订单
    return params.getParams();
  }

  @Override public void resolveResponse(String arg0) {

    OderSaleOriginal original = new Gson().fromJson(arg0, OderSaleOriginal.class);
    ArrayList<OderSaleContent> rows = original.getRows();
    if (mList == null) {
      mList = new ArrayList<>();
    }
    mList.clear();
    mList.addAll(rows);
    setAdapter(mList);
  }

  private void setAdapter(ArrayList<OderSaleContent> list) {
    if (mAdapter == null) {
      mAdapter = new OrderSaleAdapter(mActivity, list);
    }
    setListAdapter(mAdapter);
    ((OrderSaleAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_delegatetask, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item == null) return false;
    if (R.id.menu_item_search1 == item.getItemId()) {
      BaseVolleyActivity.newInstance(this, OrderQueryActivity.class, mQueryParams, SEARCH_ORDER);
      return true;
    }
    switch (item.getItemId()) {

      case R.id.menu_item_add:
        Intent i = new Intent(getActivity(), AddOrderSaleActivity.class);
        startActivityForResult(i, ADD_ORDER);
        break;
      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {

    OderSaleContent mcontent = mList.get(position - 1);
    Intent i = new Intent(getActivity(), ShowOrderSaleDeatilActivity.class);
    i.putExtra(ShowOrderSaleDeatilFragment.EXTRA_CONTENT, mcontent);
    startActivityForResult(i, DETAIL_ORDER);
    LogUtil.i("mainId====" + mcontent.getId());

    super.onListItemClick(l, v, position, id);
  }

  public void refresh() {
    volleyPost();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    //添加
    if (requestCode == ADD_ORDER) {
      volleyPost();
    }
    //查询
    if (requestCode == SEARCH_ORDER && data != null) {
      mQueryParams = (OrderQueryParams) data.getSerializableExtra(BaseQueryFragment.EXTRA_PARAMS);
      volleyPost();
    }
    //添加
    if (requestCode == DETAIL_ORDER) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
